package com.lgc.provider01.controller;

import com.lgc.provider01.entity.Product;
import com.lgc.provider01.lua.RedisLock;

import com.lgc.provider01.service.ProductService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lvguangchun
 * @email lvguangchun@konka.com
 * @date 2022-09-05 14:41
 */
@RequestMapping("/product")
@RestController
@Api(tags = "产品控制类")
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private RedisLock redisLock1;

    @GetMapping("/list")
    public List<Product> list() {
        return productService.list();
    }

    @GetMapping("/inr")
    public void inrLeft(Integer id) {
        productService.updateById(Product.builder().id(id).leftNum(10).build());
    }

    @GetMapping("/buy")
    public String buy(long time) {
        for (int i = 0; i < 100; i++) {
            try {
                new Thread(() -> redisLock1.secKill(1)).start();
                Thread.sleep(time);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "秒杀结束";
    }
}

